﻿function Main() {

this.settings = {
    gameWidth:400,
    gameHeight:300,
    invaderAttackSpeed:0.1,
    invaderSpeed:10,
    shipSpeed:110,
    invaderDropDistance:20,
    rocketSpeed:60,
    bombSpeed:65,
    invaderRows:3,
    invaderCols:5,
    pointsPerKill:10,
    fps: 50,
    difficultyUp: 0.3
    // Add amount of shots allowed in time interval?
};

this.lives = 3;
this.width = 0;
this.height = 0;
this.level= 0;
this.score= 0;
this.gameBounds = {left: 0, right: 0, top: 0, bottom: 0};
// intervalId needed?

this.dropDistance=0;

this.ship = null;
this.invaders = [];
this.rockets = [];
this.bombs = [];


function Ship(x, y) {
    this.x = x;
    this.y = y;
    this.width = 13;
    this.height = 20;
}
 

function Rocket(x, y, speed) {
    this.x = x;
    this.y = y;
    this.speed = speed;
}

function Bomb(x, y, speed) {
    this.x = x;
    this.y = y;
    this.speed = speed;
}
 
function Invader(x, y, row, col, imgRef) {
    this.x = x;
    this.y = y;
    this.row=row;
    this.col = col;
    this.width = 20;
    this.imgRef = imgRef;
    this.row = row;
    this.col = col;
    this.width = 15;
    this.height = 15;
}

function start() {
    var rows = this.settings.invaderRows;
    var cols = this.settings.invaderCols;
    var invaders = [];
    var imgObj;
    for (var i = 0; i < rows; i++) {
        for (var j = 0; j < cols; j++) {
            imgObj = document.getElementById("A" + i + "" + j);
            invaders.push(new Invader(100 + i * 25, 50 + j * 25, i, j, imgObj));
        }
    }
    this.invaders = invaders;
}

function update() {
    for (var i = 0; i < this.bombs.length; i++) {
        var bomb = this.bombs[i];
        bomb.y += this.speed;
        if (bomb.y > this.height) {
            this.bombs.splice(i--, 1);
        }
    }

    for (i = 0; i < this.rockets.length; i++) {
        var rocket = this.rockets[i];
        rocket.y -= this.speed;
        if (rocket.y < 0) {
            this.rockets.splice(i--, 1);
        }
    }

    var hitLeft=false;
    var hitRight=false;
    var hitBottom=false;
    for (i = 0; i < this.invaders.length; i++) {
        var invader = this.invaders[i];
        var movex = invader.x + this.invaderSpeed.x;
        var movey = invader.y + this.invaderSpeed.y;
        if (movex >= game.gameBounds.right) {
            hitRight = true;
        } else if (movex <= game.gameBounds.left) {
            hitLeft = true;
        } else if (movey >= game.gameBounds.bottom) {
            hitBottom = true;
        }

    }

    if (hitRight || hitLeft) {
        this.settings.invaderSpeed = -this.settings.invaderSpeed;
        for (i = 0; i < this.invaders.length; i++) {
            this.invaders[i].y += this.settings.invaderDropDistance;
        }   
    }

    if (hitBottom) {
        this.lives = 0;
    }
} 
}

}
